package com.focusai.efairy.ui.activity.announce;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import com.focusai.efairy.R;
import com.focusai.efairy.model.announce.AnnounceItem;
import com.focusai.efairy.model.announce.UpLoadAnnounceItem;
import com.focusai.efairy.model.manager.CacheManager;
import com.focusai.efairy.model.request.DeleteAnnounceReqeust;
import com.focusai.efairy.model.request.GetAnnouceDetailRequest;
import com.focusai.efairy.model.request.PostAnnounceIsReadRequest;
import com.focusai.efairy.network.HttpManager;
import com.focusai.efairy.network.Response;
import com.focusai.efairy.network.exception.NetworkException;
import com.focusai.efairy.ui.adapter.announce.ImageAdapter;
import com.focusai.efairy.ui.adapter.base.BaseRecyclerAdapter;
import com.focusai.efairy.ui.adapter.base.BaseRecyclerViewHolder;
import com.focusai.efairy.ui.adapter.base.RecycleViewDivider;
import com.focusai.efairy.ui.base.SwipeBackBaseActivity;
import com.focusai.efairy.ui.widget.FullyLinearLayoutManager;
import com.focusai.efairy.utils.ActivityUtils;
import com.focusai.efairy.utils.ScreenUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by luo_guangwen on 2017/10/20.
 */

public class AnnounceDetailActivity extends SwipeBackBaseActivity {
    public static final String KEY_ANNOUNCE_ITEM = "key_announce_item";
    public static final String KEY_ANNOUNCE_ID = "key_announce_id";
    public static final String KEY_ANNOUNCE_POSITION = "key_announce_position"; //列表的位置
    public static final String KEY_ANNOUNCE_READ = "key_announce_position"; //列表的位置
    private TextView text_title;
    private TextView text_content;
    private TextView text_company_name;
    private TextView text_send_time;
    //    private ImageView iv_announce_one;
//    private ImageView iv_announce_two;
//    private ImageView iv_announce_three;
    private RecyclerView mRecyclerView;
    private ImageAdapter mAdapter;
    private AnnounceItem announceItem;
    private int position;
    private long announcementId;
    private List<UpLoadAnnounceItem.AnnounceImageItem> mList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_announce_detail);
        if (getIntent() != null) {
            announceItem = (AnnounceItem) getIntent().getSerializableExtra(KEY_ANNOUNCE_ITEM);
            position = getIntent().getIntExtra(KEY_ANNOUNCE_POSITION, 0);
            announcementId = getIntent().getLongExtra(KEY_ANNOUNCE_ID, 0);
        }
        if (announceItem == null && announcementId == 0) {
            showToast("数据传递错误");
            finish();
            return;
        }
        initView();
        bindEvents();
        setDefaultValues();
        showData(announceItem);
        if (announcementId > 0 && announceItem == null){
            getDetailByid(announcementId);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if (announceItem != null && announceItem.efairyannouncement_publisher_id.equals(CacheManager.getUserId())) {
            MenuItem menuItem1 = menu.add(MENU_GROUP_ID, MENU_ITEM_ADD, 0, R.string.delete).setIcon(R.drawable.btn_delete);
            MenuItemCompat.setShowAsAction(menuItem1, MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
        }
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == MENU_ITEM_ADD) {
            if (announceItem != null && announceItem.efairyannouncement_id > 0) {
                delete(announceItem.efairyannouncement_id);
            }
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void initView() {
        setCenterTitleName("公告详情");
        text_title = findView(R.id.text_title);
        text_content = findView(R.id.text_content);
        text_company_name = findView(R.id.text_company_name);
        text_send_time = findView(R.id.text_send_time);
//        iv_announce_one = findView(R.id.iv_announce_one);
//        iv_announce_two = findView(R.id.iv_announce_two);
//        iv_announce_three = findView(R.id.iv_announce_three);
        mRecyclerView = findView(R.id.recycler_view);
    }

    @Override
    protected void setDefaultValues() {
        mAdapter = new ImageAdapter(getContext());
        mList = new ArrayList<>();
        mRecyclerView.setLayoutManager(new FullyLinearLayoutManager(getContext()));
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.addItemDecoration(new RecycleViewDivider(
                mContext, LinearLayoutManager.VERTICAL, ScreenUtil.dp2pix(1), getResources().getColor(R.color.main_line_gray)));
        mAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseRecyclerViewHolder holder) {
                int position = mRecyclerView.getChildLayoutPosition(holder.itemView);
                ArrayList<String> images = new ArrayList<String>();
                for (AnnounceItem.AnnounceImageItem imageItem : mList) {
                    images.add(imageItem.efairyannouncementimage_imgurl);
                }
                ActivityUtils.openPhotoPagerActivity(AnnounceDetailActivity.this, 0, position, images, false);
            }
        });
        mAdapter.refreshData(mList);

    }

    private void showData(AnnounceItem announceItem) {
        if (announceItem == null)
            return;
        setCenterTitleName(announceItem.efairyannouncement_title);
        text_title.setText(announceItem.efairyannouncement_title);
        text_content.setText(announceItem.efairyannouncement_content);
        text_company_name.setText(announceItem.efairyannouncement_publisher_nickname);
        text_send_time.setText(announceItem.efairyannouncement_add_time);
        if (announceItem.efairyannouncement_image_list != null) {
            mList.clear();
            mList.addAll(announceItem.efairyannouncement_image_list);
            mAdapter.notifyDataSetChanged();
        }
        sendIsReadRequest(announceItem);
    }

    private void sendIsReadRequest(AnnounceItem announceItem) {
        if (announceItem == null || announceItem.efairyuserhasannouncement_is_read == AnnounceItem.IS_READ) {
            return;
        }
        HttpManager.getInstance().sendRequest(new PostAnnounceIsReadRequest(announceItem.efairyannouncement_id, new Response.Listener<String>() {
            @Override
            protected void onSuccess(String response) {
                Intent intent = new Intent();
                intent.putExtra(KEY_ANNOUNCE_POSITION, position);
                intent.putExtra(KEY_ANNOUNCE_READ, true);
                setResult(RESULT_OK, intent);
            }

            @Override
            protected void onFail(NetworkException exception) {

            }
        }));
    }
    private void getDetailByid(long id){
        HttpManager.getInstance().sendRequest(new GetAnnouceDetailRequest(String.valueOf(id), new Response.Listener<AnnounceItem>() {
            @Override
            protected void onSuccess(AnnounceItem response) {
                announceItem = response;
                showData(response);
            }

            @Override
            protected void onFail(NetworkException exception) {

            }
        }));
    }
    private void delete(long announceId) {
        List<Long> ids = new ArrayList<>();
        ids.add(announceId);
        showSingleDialog("正在删除公告..");
        HttpManager.getInstance().sendRequest(new DeleteAnnounceReqeust(ids, new Response.Listener<String>() {
            @Override
            protected void onSuccess(String response) {
                closeSingleDialog();
                setResult(RESULT_OK);
                showToast("公告已删除");
                finish();
            }

            @Override
            protected void onFail(NetworkException exception) {
                closeSingleDialog();
                showToast("公告删除失败");
                doException(exception);
            }
        }));
    }
}
